(function() {
  app.controller('vipEntrustQueryCtrl', function(returnData, $scope, local_m, vipIndex, $state, bottomdilog, tradeApi, centerdilog) {
    var card_ordertype_arr = [
      '限价委托',
      '对手方最优价格委托',
      '五档即时成交剩余转限',
      '本方最优价格委托',
      '即时成交剩余撤销委托',
      '五档即时成交剩余撤销',
      '全额成交或撤销委托',
    ];

    $scope.dir_cards = {
      data: [],
      config: {
        title: {
          css: {},
          content: { 'key1': 'stkcode', 'key2': 'stkname', 'split': '--' },
        },
        rows: {
          css: {
            left: {},
            right: {
              color: '#333333'
            }
          },
          content: [
            { txt: '委托价格', key: 'price', unit: '元 / 股' },
            { txt: '委托数量', key: 'amt', unit: '股' },
            { txt: '交易方向', key: 'bstype' },
            { txt: '申报方式', key: 'card_ordertype' },
            { txt: '成交数量', key: 'matchamt', unit: '股' },
            { txt: '成交金额', key: 'matchbalance', unit: '元' },
            { txt: '委托时间', key: 'date', key2: 'time' },
          ],
        },
        buttons: {
          ok_show: false,
          cancel_show: false,
          cancel_txt: '撤单',
          cancel: function(obj) {
            $scope.cancel(obj);
          }
        },
        state: {
          show: true,
          key: 'card_state'
        }
      },
    };

    $scope.condition = [{
        head: '交易方向',
        options: [
          { txt: '全部', value: 'all' },
          { txt: '买入', value: '0' },
          { txt: '卖出', value: '1' },
        ],
        default: { txt: '全部', value: 'all' },
      },
      {
        head: '委托日期',
        options: [
          { txt: '全部', value: 90 },
          { txt: '当天', value: 1 },
          { txt: '7天之内', value: 7 },
          { txt: '15天之内', value: 15 },
        ],
        default: { txt: '当天', value: 1 },
      }
    ];

    $scope.init = function() {
      var data = returnData.dateSet($scope.condition[1].default.value, 'yyyyMMdd');
      data.funcid = '2';
      $scope.client_deal_query(data);
    };

    $scope.cancel = function(obj) {
      centerdilog
        .open({
          msg: '确定撤销该委托吗？',
          ok: function() {
            tradeApi
              .etQuery({
                funcid: '11',
                orderno: obj.obj.sno
              })
              .then(function(res) {
                centerdilog
                  .open({
                    msg: '撤销申请提交成功！'
                  });
              });
          }
        });

    };

    $scope.client_deal_query = function(data) {
      tradeApi
        .etQuery(data)
        .then(function(res) {
          angular.forEach(res, function(item) {
            item.card_ordertype = card_ordertype_arr[item.ordertype];
            if (item.cancelflag === '未撤单') {
              if (item.cancancel === '可撤单') {
                item.card_state = '委托中';
                item.cancel_button_show = true;
              }
              if (item.cancancel === '不可撤单') {
                item.card_state = '全部成交';
              }
            }

            if (item.cancelflag === '已撤单') {
              if (item.cancancel === '不可撤单') {
                item.card_state = '已撤单';
              }
            }
          });
          if ($scope.condition[0].default.txt !== '全部') {
            for (var i = 0; i < res.length; i++) {
              if (res[i].bstype !== $scope.condition[0].default.txt) {
                res.splice(i, 1);
                i--;
              }
            }
          }
          $scope.dir_cards.data = res;
        });
    };
  });

})();